<style>
  .layui-table-cell .layui-table-sort {
    display: none;
  }

  .layui-table-view .layui-table-tool-self {
    display: flex;
    justify-content: flex-end;
    position: initial;
    margin-top: 12px;
  }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label">开工单号：</label>
                  <div class="layui-input-inline">
                    <input name="workOrderCode" id="workOrderCode" class="layui-input" type="text" placeholder="请输入开工单号" autocomplete="off" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">所属计划：</label>
                  <div class="layui-input-inline">
                    <input name="productionPlanCode" id="productionPlanCode" class="layui-input" type="text" placeholder="请输入所属计划" autocomplete="off" />

                    <!-- <select id="planId" name="planId" lay-filter="planId" lay-search class="common-select-search"></select> -->
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">所属制造令：</label>
                  <div class="layui-input-inline">
                    <input name="productionRecordCode" id="productionRecordCode" class="layui-input" type="text" placeholder="请输入所属计划" autocomplete="off" />

                    <!-- <select id="planId" name="planId" lay-filter="planId" lay-search class="common-select-search"></select> -->
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">所属物料：</label>
                  <div class="layui-input-inline">
                    <input name="skuCode" id="skuCode" class="layui-input" type="text" placeholder="请输入所属物料" autocomplete="off" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">所属车间：</label>
                  <div class="layui-input-inline">
                    <select id="workshopIdchejian" name="workshopId" lay-filter="workshopId" lay-search class="common-select-search"></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">计划状态：</label>
                  <div class="layui-input-inline">
                    <select id="planStatus" name="planStatus" lay-filter="planStatus" class="common-select-search"></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">领取时间：</label>
                  <div class="layui-input-inline">
                    <input id="pTime" class="layui-input date-icon" type="text" placeholder="选择时间" style="width: 200px" readonly />
                  </div>
                  <input name="startCreateTime" id="sTime" type="hidden" />
                  <input name="endCreateTime" id="eTime" type="hidden" />
                </div>
              </div>
              <div class="query-rt">
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-reseda" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="receiptRecordAddByLast" type="button" data-type="last" class="layui-btn icon-btn">添加上道工序领料</button>
              <button id="receiptRecordAdd" type="button" data-type="normal" class="layui-btn icon-btn">添加物料领料</button>
            </div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table-bor">
        <table class="layui-table" id="receiptRecordTable" lay-filter="receiptRecordTable"></table>
      </div>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="receiptRecordTabBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
  <!-- {{# if(d.status == '1'){ }} -->
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="audit">审核</a>
  <!-- {{# } }} -->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="receiptRecordForm">
  <form
    lay-filter="receiptRecordForm"
    id="receiptRecordFormContent"
    class="layui-form open-form"
  >
    <div class="open-model">
      <div class="open-auto">
        <input name="id" type="hidden" />
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料类型</label>
            <div class="layui-input-block">
              <select
                id="inventoryTypeId"
                name="inventoryTypeId"
                lay-filter="inventoryTypeId"
                disabled
                lay-search
              ></select>
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">开工单编号</label>
            <div class="layui-input-block">
              <input
                id="productionPlanRecordId"
                name="productionPlanRecordId"
                type="hidden"
              />
              <input id="processId" name="processId" type="hidden" />
              <select
                id="workOrderId"
                name="workOrderId"
                lay-filter="workOrderId"
                lay-search
              ></select>
            </div>
          </div>
        </div>
        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">工站任务编号</label>
            <div class="layui-input-block">
              <input
                id="productionProcessDeviceCode"
                name="productionProcessDeviceCode"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div> -->
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label" "
              >工位属性：</label
            >
            <div class="layui-input-block" >
              <input
                name="typeName"
                class="layui-input"
                type="text"
                disabled
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label" "
              >工位性质：</label
            >
            <div class="layui-input-block" >
              <input name="workstationNature" type="hidden" />
              <input
                name="workstationNatureName"
                class="layui-input"
                type="text"
                disabled
                readonly="readonly"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label" "
              >设备/工种：</label
            >
            <div class="layui-input-block" >
              <select
                id="gzsbId"
                name="gzsbId"
                lay-filter="gzsbId"
                lay-search
              ></select>
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">工位：</label>
            <div class="layui-input-block" >
              <input type="hidden" id="workshopName" name="workshopName" />
              <select
                id="workshopId"
                name="workshopId"
                lay-filter="workshopId"
                lay-search
              ></select>
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">所属计划</label>
            <div class="layui-input-block">
              <input
                id="planCode"
                name="planCode"
                type="text"
                class="layui-input"
                disabled
              />
            </div>
          </div>
        </div>

        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">仓库类型</label>
            <div class="layui-input-block">
              <select
                id="warehouseType"
                name="warehouseType"
                lay-filter="warehouseType"
              ></select>
            </div>
          </div>
        </div> -->
        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料仓库</label>
            <div class="layui-input-block">
              <select
                id="receiptWarehouse"
                name="receiptWarehouse"
                lay-filter="receiptWarehouse"
                lay-search
              ></select>
            </div>
          </div>
        </div> -->
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料料号数</label>
            <div class="layui-input-block">
              <input
                id="receiptNum"
                name="receiptNum"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核人</label>
            <div class="layui-input-block">
              <select
                id="auditPerson"
                name="auditPerson"
                lay-filter="auditPerson"
                lay-search
              ></select>
            </div>
          </div>
        </div>
        <div class="layui-col-xs4" id="creatorNameDiv">
          <div class="layui-form-item">
            <label class="layui-form-label">领料人</label>
            <div class="layui-input-block">
              <input
                id="creatorName"
                name="creatorName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
              <input
                id="remark"
                name="remark"
                type="text"
                class="layui-input"
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">上道工序合格数</label>
            <div class="layui-input-block">
              <input
                name="receiptRecord"
                type="text"
                class="layui-input"
                disabled
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领取上道工序数</label>
            <div class="layui-input-block">
              <input
                name="getQuantity"
                type="text"
                class="layui-input"
              />
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="addReceiptRecordDetailForm-title">
            <h3 class="title">领料明细</h3>
            <button
              class="layui-btn addTable addReceiptRecordDetail"
              type="button"
              style="margin-top: 10px;float: right;margin-right: 20px;"
            >
              添加领料信息
            </button>
          </div>
          <div class="line"></div>
          <!-- 表格 -->
          <table
            class="layui-table"
            id="receiptRecordDetailTable"
            lay-filter="receiptRecordDetailTable"
          ></table>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button
          class="layui-btn layui-btn-primary"
          type="button"
          ew-event="closeDialog"
        >
          取消
        </button>
        <button
          class="layui-btn"
          lay-filter="receiptRecordFormSubmit"
          lay-submit
        >
          保存
        </button>
      </div>
    </div>
  </form>
</script>

<!--领料明细表格操作列-->
<script type="text/html" id="receiptRecordDetailTableBar">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 领料记录审核、详情弹窗 -->
<script type="text/html" id="receiptRecordEditForm">
  <form lay-filter="receiptRecordEditForm" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto">
        <input name="id" type="hidden" />
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料类型</label>
            <div class="layui-input-block">
              <input name="inventoryTypeName" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">开工单编号</label>
            <div class="layui-input-block">
              <input name="workOrderCode" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">工站任务编号</label>
            <div class="layui-input-block">
              <input name="productionProcessDeviceCode" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">所属计划</label>
            <div class="layui-input-block">
              <input name="productionPlanCode" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">仓库类型</label>
            <div class="layui-input-block">
              <input name="warehouseTypeName" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料仓库</label>
            <div class="layui-input-block">
              <input
                name="receiptWarehouseName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div> -->
        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料料号数</label>
            <div class="layui-input-block">
              <input
                name="receiptNum"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div> -->
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核人</label>
            <div class="layui-input-block">
              <input name="auditPersonName" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">领料人</label>
            <div class="layui-input-block">
              <input name="creatorName" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
              <input name="remark" type="text" class="layui-input" />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核结果</label>
            <div class="layui-input-block">
              <textarea name="auditResult" placeholder="请输入审核结果" maxlength="200" class="layui-textarea"></textarea>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="addReceiptRecordDetailForm-title" style="margin-left: 0;">
            <h3 class="title">领料明细</h3>
          </div>
          <div class="line"></div>
          <!-- 表格 -->
          <table class="layui-table" id="receiptRecordDetailEditTable" lay-filter="receiptRecordDetailEditTable"></table>
        </div>
      </div>
      <div class="layui-form-item text-right" id="receiptRecordFormAuditDiv">
        <button class="layui-btn" lay-filter="auditSuccessBtn" type="button" lay-submit>通过</button>
        <button class="layui-btn layui-btn-danger" lay-filter="auditFailBtn" type="button" lay-submit>驳回</button>
      </div>
    </div>
  </form>
</script>

<!--物料弹窗-->
<script type="text/html" id="productDetailForm">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <!-- 头部操作栏 -->
        <div class="layui-form toolbar">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料类别</label>
              <div class="layui-input-inline">
                <input id="productTypeId" type="hidden" name="productTypeId" />
                <div id="selectProductType"></div>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料编码</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编码" />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料规格</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input name="skuSpec" class="layui-input" type="text" placeholder="请输入物料规格" />
              </div>
            </div>
            <div class="layui-inline">
              <button class="layui-btn icon-btn" lay-filter="productDetailSearch" lay-submit style="padding: 0 24px;">搜索</button>
              <button class="layui-btn icon-btn" lay-filter="productDetailResets" lay-submit style="padding: 0 24px;">重置</button>
              <button id="productDetailConfirm" class="layui-btn icon-btn">确认选择</button>
            </div>
          </div>
        </div>
        <!-- 表格 -->
        <div class="scroll-table-4-5">
          <table class="layui-table" id="productDetailTable" lay-filter="productDetailTable"></table>
        </div>
      </div>
    </div>
  </div>
</script>
<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "laydate", "admin", "laytpl", "tableTreeDj", "config"], async function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var table = layui.table;
    var form = layui.form;
    var config = layui.config;
    var admin = layui.admin;
    var tableTree = layui.tableTreeDj;

    let workObj = null;
    let nowData = null;
    let where = {inventoryTypeId: 5};
    if (typeof parentData !== "undefined") {
      nowData = { ...parentData };
      where = { ...nowData.where };
    }

    laydate.render({
      elem: "#pTime",
      type: "date",
      range: true,
      done: function (value, date, endDate) {
        if (value) {
          var dateValue = value.split(" - ");
          $("#sTime").val(dateValue[0] + " 00:00:00");
          $("#eTime").val(dateValue[1] + " 23:59:59");
        } else {
          $("#sTime").val();
          $("#eTime").val();
        }
      }
    });
    admin.initDataSelectPro("enterprise/device/admin/deviceWorkshop/list", {}, "workshopIdchejian", "车间", () => {}, "name");
    //所属计划列表
    // admin.initDataSelect(
    //   "production/productionPlan/admin/productionPlan/list",
    //   null,
    //   "planId",
    //   null,
    //   "所属计划",
    //   "code",
    //   null
    // );
    //所属物料列表
    // admin.initDataSelect(
    //   "enterprise/product/admin/productDetail/list",
    //   null,
    //   "productDetailId",
    //   null,
    //   "所属物料",
    //   "skuName",
    //   null
    // );
    //物料选择事件-所属工序列表
    form.on("select(productDetailId)", function (selectData) {
      $("#processId").empty();
      admin.initDataSelect("enterprise/echnologyParam/admin/echnologyParam/list", { productDetailId: selectData.value }, "processId", null, "所属工序", null, null);
    });
    //计划状态
    admin.getDictList("planStatus", "production_plan_status", null, "计划状态");
    // 渲染表格
    var insTb = table.render({
      id: "#receiptRecordTable",
      elem: "#receiptRecordTable",
      url: config.base + "enterprise/receiptRecord/admin/receiptRecord/list",
      page: true,
      where: { ...where },
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers", title: "序号", align: "center" },
          { field: "code", title: "领料单编号", align: "center", width: 160 },
          {
            field: "workOrderCode",
            title: "开工单编号",
            align: "center",
            width: 150
          },
          {
            field: "productionRecordCode",
            title: "制造令编号",
            align: "center",
            width: 150
          },
          {
            field: "productionPlanCode",
            align: "center",
            title: "所属计划",
            width: 150
          },
          {
            field: "skuCode",
            align: "center",
            title: "所属物料"
          },
          {
            field: "workshopName",
            align: "center",
            title: "所属车间"
          },
          // { field: "warehouseTypeName", align: "center", title: "仓库类型" },
          // {
          //   field: "receiptWarehouseName",
          //   align: "center",
          //   title: "领料仓库",
          // },
          // { field: "receiptNum", align: "center", title: "领料料号数" },
          { field: "statusName", align: "center", title: "状态" },
          { field: "auditPersonName", align: "center", title: "审核人" },
          { field: "creatorName", align: "center", title: "领料人" },
          {
            field: "createTime",
            align: "center",
            title: "领取时间",
            width: 160
          },
          {
            align: "center",
            toolbar: "#receiptRecordTabBar",
            title: "操作",
            width: 160,
            fixed: "right"
          }
        ]
      ],
      done: function (res, curr, count) {}
    });

    let materialData = [];

    //初始化产品类别
    // admin.initDataSelectTree(
    //   true,
    //   "enterprise/product/admin/productType/getTreeData",
    //   { limit: 99999 },
    //   "productType",
    //   null,
    //   "产品类别",
    //   "productTypeId",
    //   function (row) {
    //     $("input[name='productTypeId']").val(row.arr[0].value);
    //   }
    // );

    //渲染下拉
    form.render("select");

    // 工具条点击事件
    table.on("tool(receiptRecordTable)", function (obj) {
      var data = obj.data;
      if (obj.event === "audit") {
        //审核
        // showModel(data, 1);
        admin
          .reqPro("enterprise/receiptRecord/admin/receiptRecord/get", {
            id: data.id
          })
          .then((res) => {
            openForm("audit", res.data);
          });
        // showModel(data, 1);
      } else if (obj.event === "detail") {
        //详情
        // showModel(data, 2);
        admin
          .reqPro("enterprise/receiptRecord/admin/receiptRecord/get", {
            id: data.id
          })
          .then((res) => {
            openForm("detail", res.data);

            // showModel(res.data, 1);
          });
      }
    });

    //监听排序
    table.on("sort(receiptRecordTable)", function (obj) {
      table.reload("receiptRecordTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type
        }
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", (data) => {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
    });
    const openForm = (eventName, data) => {
      let title = "";
      if (eventName === "add" || eventName === "addLast") {
        title = "添加领料物料记录";
      } else if (eventName === "audit") {
        title = "审核领料记录";
      } else if (eventName === "detail") {
        title = "领料记录详情";
      }
      admin.formOpen({
        type: 1,
        area: ["1500px", "90%"],
        style: "font-size: 20px;",
        title: title,
        path: "components/res/receiptRecord/receiptRecordForm.html",
        success: function (layero, index) {
          parentData.data = data;
          parentData.eventName = eventName;
        }
      });
    };
    // 添加按钮点击事件
    $("#receiptRecordAddByLast").click(function () {
      openForm("addLast");
      // showEditModel();
    });
    $("#receiptRecordAdd").click(function () {
      openForm("add");
      // openForm("add",'normal');
      // showEditModel();
    });

    // 领料记录弹窗
    function showEditModel() {
      admin.open({
        type: 1,
        area: ["1500px", "90%"],
        style: "font-size: 20px;",
        title: "添加领料记录",
        content: $("#receiptRecordForm").html(),
        success: async function () {
          //领料人
          $("#creatorNameDiv").hide();
          //开工单列表
          await admin.initDataSelectPro(
            "production/productionPlan/admin/productionWorkOrder/list",
            {
              limit: 99999,
              statusFlag: 1,
              status: "inproduction"
            },
            "workOrderId",
            "开工单",
            async (d, obj) => {
              form.val("receiptRecordForm", {
                productionPlanRecordId: "",
                processId: "",
                workstationNature: "",
                workstationNatureName: "",
                gzsbId: "",
                workshopId: "",
                workshopName: "",
                planCode: ""
              });
              $("#gzsbId").empty();
              workObj = null;
              if (d.value) {
                form.val("receiptRecordForm", {
                  productionPlanRecordId: "",
                  processId: ""
                });
                const res1 = await admin.reqPro("production/productionPlan/admin/productionWorkOrder/detail/" + obj.id);
                workObj = res1.data;
                const res2 = await admin.reqPro("enterprise/res/admin/setting/workStation/list", {
                  professionId: res1.data.workstationNature == 1 ? res1.data.gzsbId : "",
                  deviceModelSpuId: res1.data.workstationNature == 2 ? res1.data.gzsbId : ""
                });
                admin.initDataSelect(res1.data.deviceTypeList, null, "gzsbId", null, "设备机型", "deviceName", "id");
                form.on("select(gzsbId)", async function (d) {
                  $("#workshopId").empty();
                  $("#workshopId").val("");
                  form.render("select");
                  let workstationNature = $("#workstationNature").val();
                  if (d.value) {
                    console.log(d.value);

                    const res1 = await admin.reqPro("enterprise/res/admin/setting/workStation/list", {
                      // gzsbId: d.value,
                      professionId: workstationNature == 1 ? d.value : "",
                      deviceModelSpuId: workstationNature == 2 ? d.value : ""
                    });
                    admin.initDataSelect(res1.data, null, "workshopId", null, "工位", "name", null);
                    form.on("select(workshopId)", async function (d) {
                      $("#workshopName").val();
                      $("#workshopName").val(d.elem[d.elem.selectedIndex].text);
                    });
                  }
                });

                // admin.req(
                //   "production/productionPlan/admin/productionWorkOrder/getWorkOrderDetails",
                //   { workOrderId: d.value },
                //   function (res) {
                //     $("#productionProcessDeviceCode").val(
                //       res.data.productionProcessDeviceCode
                //     );
                //     $("#planCode").val(res.data.planCode);
                //   }
                // );
                form.val("receiptRecordForm", {
                  productionPlanRecordId: obj.productionPlanRecordId,
                  processId: obj.processId
                });
                form.val("receiptRecordForm", { ...res1.data });
              }
            },
            "code"
          );

          //仓库类型
          // admin.initDataSelect(
          //   "enterprisecab/warehouse/admin/warehouseType/list",
          //   { limit: 99999 },
          //   "warehouseType",
          //   null,
          //   "仓库类型",
          //   null,
          //   null
          // );

          //初始化领料类型
          // 默认为领料出库 valye为5
          admin.initDataSelect("enterprisecab/warehouse/admin/warehouseInventoryType/list", { limit: 99999 }, "inventoryTypeId", 5, "领料类型", null, null);

          //审核人
          admin.initDataSelect("user/admin/user/list", { limit: 99999 }, "auditPerson", null, "审核人", "nickname", null);

          //开工单编号选择事件
          //  form.on("select(workOrderId)", function (selectData) {
          //    admin.req("production/productionPlan/admin/productionWorkOrder/getWorkOrderDetails", { workOrderId: selectData.value }, function (res) {
          //      $("#productionProcessDeviceCode").val(res.data.productionProcessDeviceCode);
          //      $("#planCode").val(res.data.planCode);
          //    });
          //  });

          //仓库类型选择事件
          // form.on("select(warehouseType)", function (selectData) {
          //   $("#receiptWarehouse").empty();
          //   admin.initDataSelect(
          //     "enterprisecab/warehouse/admin/warehouse/list",
          //     {
          //       limit: 99999,
          //       warehouseTypeId: selectData.value,
          //     },
          //     "receiptWarehouse",
          //     null,
          //     "领料仓库",
          //     null,
          //     null
          //   );
          // });

          //领料明细表格
          table.render({
            elem: "#receiptRecordDetailTable",
            data: [],
            cellMinWidth: 100,
            page: false,
            cols: [
              [
                { type: "numbers", width: 50, title: "序号" },
                { field: "skuCode", title: "物料编码", align: "center" },
                {
                  field: "skuName",
                  title: "物料名称",
                  align: "center",
                  width: 150
                },
                {
                  field: "skuSpec",
                  title: "物料规格",
                  align: "center",
                  width: 150
                },
                {
                  field: "materialNatureName",
                  title: "物料类别性质",
                  align: "center",
                  width: 140
                },
                {
                  field: "productTypeName",
                  title: "物料类别",
                  align: "center",
                  width: 140
                },
                { field: "baseUnitName", title: "基础单位", align: "center" },
                { field: "unitName", title: "计量单位", align: "center" },
                {
                  field: "haveQuantity",
                  title: "库存数量",
                  align: "center",
                  edit: "text"
                },
                {
                  field: "receiptNum",
                  title: "领取数量",
                  align: "center",
                  width: 140,
                  edit: "text"
                },
                {
                  field: "remark",
                  title: "备注",
                  align: "center",
                  width: 160,
                  edit: "text"
                },
                {
                  align: "center",
                  toolbar: "#receiptRecordDetailTableBar",
                  title: "操作",
                  width: 100
                }
              ]
            ]
          });

          // 领料明细工具条点击事件
          table.on("tool(receiptRecordDetailTable)", function (obj) {
            let rowData = obj.data;
            let list = materialData;
            if (obj.event === "save") {
              // 保存
              let planProductionQuantity = $(this).parents("td").siblings().find(".planProductionQuantity").val();
              let outputQuantity = $(this).parents("td").siblings().find(".outputQuantity").val();
              if (planProductionQuantity == "") {
                layer.msg("请输入计划排产数量", { icon: 2 });
                return false;
              }
              if (outputQuantity == "") {
                layer.msg("请输入排产数量", { icon: 2 });
                return false;
              }
              list.forEach((item, index) => {
                if (rowData.deviceCode === item.deviceCode) {
                  list[index].status = "yes";
                  list[index].outputQuantity = outputQuantity;
                  list[index].planProductionQuantity = planProductionQuantity;
                }
              });
            } else if (obj.event === "del") {
              //删除
              list.forEach((item, index) => {
                if (rowData.skuCode === item.skuCode) {
                  list.splice(index, 1);
                }
              });
              //删除后更新数据
              materialData = list;
            }
            // 渲染表格
            table.reload("receiptRecordDetailTable", {
              data: list
            });
          });

          // 添加领料信息
          $(".addReceiptRecordDetail").click(function () {
            if (!$("#workOrderId").val()) {
              layer.msg("请先选择开工单", { icon: 2 });
              return;
            }
            if (!$("#receiptWarehouse").val()) {
              layer.msg("请先选择仓库", { icon: 2 });
              return;
            }

            console.log(123, workObj);

            admin.formOpen({
              area: "90%",
              offset: "50px",
              title: "配置物料清单",
              path: "components/purchase/skuTableForm.html",
              success: function () {
                console.log(123, workObj);
                let inIds = workObj.skuList.map((v) => v.productDetailId).join(",");
                parentData.where = {
                  inIds,
                  warehouseId: $("#receiptWarehouse").val(),
                  notInListIds: table.cache["receiptRecordDetailTable"].map((v) => v.productDetailId).join(",")
                };
                // parentData.notInListIds = table.cache["skuTable"]
                //   .map((v) => v.productDetailId)
                //   .join(",");
              },
              end: function () {
                if (childData.list) {
                  const newData = childData.list || [];
                  const oldData = table.cache["receiptRecordDetailTable"];
                  const list = [...oldData, ...newData];
                  table.reload("receiptRecordDetailTable", {
                    data: list // 设置新的数据
                  });
                }
              }
            });
          });
        },
        finish: function () {
          table.reload("receiptRecordTable", {});
        }
      });
    }

    // 领料记录审核、详情弹窗
    function showModel(data, type) {
      admin.open({
        type: 1,
        area: ["1500px", "90%"],
        style: "font-size: 20px;",
        title: type === 1 ? "领料记录审核" : "领料记录详情",
        content: $("#receiptRecordEditForm").html(),
        success: function () {
          //详情页面-隐藏按钮
          if (type === 2) {
            $("#receiptRecordFormAuditDiv").hide();
          }
          //领料记录数据回显
          form.val("receiptRecordEditForm", data);
          form.render();
          //领料明细表格
          table.render({
            elem: "#receiptRecordDetailEditTable",
            data: data.receiptRecordDetailList,
            cellMinWidth: 100,
            page: false,
            cols: [
              [
                { type: "numbers", width: 50, title: "序号" },
                { field: "skuCode", title: "物料编码", align: "center" },
                {
                  field: "skuName",
                  title: "物料名称",
                  align: "center",
                  width: 150
                },
                {
                  field: "skuSpec",
                  title: "物料规格",
                  align: "center",
                  width: 150
                },
                {
                  field: "materialNatureName",
                  title: "物料类别性质",
                  align: "center",
                  width: 140
                },
                {
                  field: "productTypeName",
                  title: "物料类别",
                  align: "center",
                  width: 140
                },
                { field: "baseUnitName", title: "基础单位", align: "center" },
                { field: "unitName", title: "计量单位", align: "center" },
                {
                  field: "receiptNum",
                  title: "领取数量",
                  align: "center",
                  width: 140,
                  edit: "text"
                },
                {
                  field: "remark",
                  title: "备注",
                  align: "center",
                  width: 160,
                  edit: "text"
                }
              ]
            ]
          });

          //审核通过
          form.on("submit(auditSuccessBtn)", function (data) {
            data.field.status = 2; //待领料
            // 更新
            admin.req(
              "enterprise/receiptRecord/admin/receiptRecord/update",
              data.field,
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  layer.closeAll();
                  insTb.reload("receiptRecordTable");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "POST"
            );
            return false;
          });

          //审核驳回
          form.on("submit(auditFailBtn)", function (data) {
            data.field.status = 4; //驳回
            // 更新
            admin.req(
              "enterprise/receiptRecord/admin/receiptRecord/update",
              data.field,
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  layer.closeAll();
                  insTb.reload("receiptRecordTable");
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "POST"
            );
            return false;
          });
        },
        finish: function () {
          table.reload("receiptRecordTable", {});
        }
      });
    }

    // 提交数据
    form.on("submit(receiptRecordFormSubmit)", function (d) {
      let flag = false;
      // if (materialData.length == 0) {
      //   layer.msg("请添加领料信息", { icon: 2 });
      //   return false;
      // }
      //领料明细列表
      var detailList = table.cache["receiptRecordDetailTable"];
      detailList.forEach((detail) => {
        if (detail.receiptNum === "" || detail.receiptNum === null || detail.receiptNum === undefined) {
          layer.msg(`物料【${detail.skuName}】领取数量不能为空`, { icon: 2 });
          flag = true;
          return false;
        }
      });
      // 控制不能提交为空的数据
      if (flag) {
        return false;
      }
      d.field.receiptRecordDetailList = detailList;
      // 提交计划
      admin.req(
        "enterprise/receiptRecord/admin/receiptRecord/add",
        JSON.stringify(d.field),
        function (res) {
          layer.closeAll("loading");
          if (res.code == 200) {
            // 提交成功清空缓存数据
            materialData = [];
            layer.msg(res.msg, { icon: 1 });
            layer.closeAll();
            insTb.reload("receiptRecordTable");
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "POST",
        true,
        "application/json; charset=utf-8"
      );
      return false;
    });
  });
</script>
<style>
  .addReceiptRecordDetailForm-title {
    width: 100%;
    margin-left: 20px;
  }

  .addReceiptRecordDetailForm-title .title,
  .title {
    display: inline-block;
    font-size: 16px;
    font-weight: bold;
    color: #333333;
    line-height: 19px;
    position: relative;
    padding: 6px 0 19px 0;
  }

  .addReceiptRecordDetailForm-title .title:after,
  .title:after {
    content: "";
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    width: calc(100% + 6px);
    height: 3px;
    background: #0a8b0a;
  }

  .addTable {
    height: 30px;
    margin-left: 30px;
    line-height: 30px;
    padding: 0 10px;
  }
</style>
